{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Vector field animation\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import k3d\n",
    "import numpy as np\n",
    "import time \n",
    "\n",
    "plot = k3d.plot()\n",
    "\n",
    "Nx,Ny = 10,10\n",
    "\n",
    "x = np.linspace(-1,1,Nx,dtype=np.float32)\n",
    "y = np.linspace(-2,2,Ny,dtype=np.float32)\n",
    "\n",
    "x,y = np.meshgrid(x,y)\n",
    "z = np.zeros_like(x)\n",
    "\n",
    "origins = np.vstack([x.flatten(),y.flatten(),z.flatten()]).T\n",
    "vectors = 0.5*(np.vstack([np.zeros_like(x.flatten()),\n",
    "                          np.zeros_like(x.flatten()),\n",
    "                          np.ones_like(x.flatten())] )).T\n",
    "phi0 = np.linspace(0,2*np.pi,vectors.shape[0])\n",
    "labels = [str(i) for i in range(vectors.shape[0])]\n",
    "vector_plot = k3d.vectors(origins.astype(np.float32), vectors.astype(np.float32))\n",
    "\n",
    "plot += vector_plot\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.camera_auto_fit = False\n",
    "plot.grid_auto_fit = False\n",
    "\n",
    "for phi in np.linspace(0,2*np.pi,55):\n",
    "    r = 0.1\n",
    "    vector_plot.vectors = np.stack([r*np.cos(phi+phi0),\n",
    "                                    r*np.sin(phi+phi0),\n",
    "                                    0.4*np.ones_like(phi0)]).T.astype(np.float32)\n",
    "    time.sleep(0.05) # have to wait for data update"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "nbTranslate": {
   "displayLangs": [
    "en",
    "pl"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "pl",
   "targetLang": "en",
   "useGoogleTranslate": true
  },
  "widgets": {
   "state": {
    "e8e906fba93843f3af98e01c81918353": {
     "views": [
      {
       "cell_index": 2
      }
     ]
    }
   },
   "version": "1.2.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
